//
//  XCRecentPlayViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/22.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCRecentPlayViewController.h"
#import "XCRecentPlayTableViewCell.h"
#import "WDGDatabaseTool.h"
#import "UIImageView+WebCache.h"
#import "XCPlayViewController.h"
#import "OnlineAVplay.h"
@interface XCRecentPlayViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) WDGDatabaseTool *recentPlay;
@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme;
@property (nonatomic, retain) NSMutableArray *mutableArraySongName;
@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;
@property (nonatomic, retain) NSMutableArray *nameArray;
@end

@implementation XCRecentPlayViewController
- (void)dealloc {
    _tableView.dataSource = nil;
    _tableView.delegate = nil;
    
    [_tableView release];
    [_dataSource release];
    [_mutableArraySingerNAme release];
    [_mutableArraySongName release];
    [_nameArray release];
    [_mutableHeadIVPicurl release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  
    self.recentPlay = [WDGDatabaseTool DBManageWithTableName:@"recentPlay"];
    [self.recentPlay openDatabase];
    self.dataSource = [NSMutableArray arrayWithArray:[[self.recentPlay orderWithDictionary:@{@"id":@"desc"}] selectAllData]];
    
    [self.recentPlay closeDatabase];
    
    for (NSDictionary *dic in self.dataSource) {
        [self.mutableArraySongName addObject:[dic objectForKey:@"url"]];
        [self.mutableArraySingerNAme addObject:[dic objectForKey:@"singername"]];
        [self.mutableHeadIVPicurl addObject:[dic objectForKey:@"picurl"]];
        [self.nameArray addObject:[dic objectForKey:@"songname"]];
    }

    [self.tableView reloadData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableViewBuild];
    self.mutableArraySingerNAme = [NSMutableArray arrayWithCapacity:0];
    self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
    self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
    self.nameArray = [NSMutableArray arrayWithCapacity:0];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-clearAll"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction)];

    self.recentPlay = [WDGDatabaseTool DBManageWithTableName:@"recentPlay"];
    [self.recentPlay openDatabase];
    self.dataSource = [NSMutableArray arrayWithArray:[[self.recentPlay orderWithDictionary:@{@"id":@"desc"}] selectAllData]];
    [self.recentPlay closeDatabase];
//
    for (NSDictionary *dic in self.dataSource) {
        [self.mutableArraySongName addObject:[dic objectForKey:@"url"]];
        [self.mutableArraySingerNAme addObject:[dic objectForKey:@"singername"]];
        [self.mutableHeadIVPicurl addObject:[dic objectForKey:@"picurl"]];
        [self.nameArray addObject:[dic objectForKey:@"songname"]];
    }
//
    [self.tableView reloadData];
}
- (void)tableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[XCRecentPlayTableViewCell class] forCellReuseIdentifier:@"XCRecentPlayTableViewCell"];
    [_tableView release];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCRecentPlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCRecentPlayTableViewCell"];
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:self.mutableHeadIVPicurl[indexPath.row]] placeholderImage:[UIImage imageNamed:@"Image-p"]];
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.songLable.text = self.nameArray[indexPath.row];
    cell.singerLable.text = self.mutableArraySingerNAme[indexPath.row];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY/MMM/dd   HH:mm:ss"];
    
    NSString *datetimeNow = [formatter stringFromDate:[NSDate date]];
    cell.dataLable.text = datetimeNow;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    

    [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row;
    [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
    [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
    [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme;
    [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
    
    NSDictionary *dic = self.dataSource[indexPath.row];
    [[OnlineAVplay shareAVPlayer] playWithUrl:[dic objectForKey:@"url"]];
    [[OnlineAVplay shareAVPlayer] play];
    
    [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightBarButtonItemAction {
    [self.recentPlay openDatabase];
    [self.recentPlay deleteDataWithWhereCondition:nil];
    if ([self.recentPlay deleteDataWithWhereCondition:nil]) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }
    [self.recentPlay closeDatabase];
//    [self.tableView reloadData];
    [self.tableView removeFromSuperview];
}

@end
